/**
 * @author TPS
 */
window.onload = function(){
    /*Textarea HTMLElement Finding*/
    var txtArea = document.getElementById('ta1');
    
    // Buttons identificators    
    var btnEditAB = document.getElementById('b1');
    var btnJSON = document.getElementById('b2');
    
    
/*=================JSON AdBlock validate function(): is it clear AdBlock (properties, values, types)?===========*/
    function isAdBlock(json_object){
    
        /*Array: defines AdBlock structure (properties) for checking*/
        var AdBlockProps = ["title", "x", "y", "width", "height", "color", "image", "text"];
        
        //Loop: checks JSON-block for props prescence (nalichie) and its type (correct?)
        for (var i = 0; i < AdBlockProps.length; i++) {
        
            if (AdBlockProps[i] in json_object) {
            
                if ((AdBlockProps[i] == "title") || (AdBlockProps[i] == "color") || (AdBlockProps[i] == "image") || (AdBlockProps[i] == "text")) {
                
                    if (typeof(json_object[AdBlockProps[i]]) !== "string") 
                        alert("Value of " + AdBlockProps[i] + ": " + json_object[AdBlockProps[i]] + " is not string!");
                }
                else 
                    //If x,y,width or height property (for Number-or-not type checking)
                    if ((AdBlockProps[i] == "x") || (AdBlockProps[i] == "y") || (AdBlockProps[i] == "width") || (AdBlockProps[i] == "height")) {
                    
                        //RegExp checks that property of AB from ta consist of numbers
                        if (json_object[AdBlockProps[i]].toString().search(/\d+/) == -1) {
                            alert(AdBlockProps[i] + " must consist of numbers!");
                        }
                    }
                    else 
                        alert('Property: ' + AdBlockProps[i] + ' expected!');
                
            }
        }
    }

/*===============================================BUTTON [Edit AdBlock]=========================================*/
    btnEditAB.onclick = function(ta_content){
    
        if (txtArea.value.replace(/\s+/g, '').length) {
            //RegExp: checking that string is not empty (and delete all spaces = replace(/\s+/g))
            
            // Parsing JSON from TextArea field and checking it for AdBlock structure
            try {
            
                var json_tav = JSON.parse(txtArea.value);
                isAdBlock(json_tav);   //function call: foo describes here!
				
				var winTab = window.open("winTab","width=400,height=300,status=yes,resizable=yes");
                
            } 
            catch (SyntaxError) {
                alert('Its not JSON code!')
            }
        }
        else {
            alert('The entry field is empty! Please, enter JSON code of AdBlock!');
        }
    }
    
/*===============================================BUTTON [Load JSON]===============================================*/
    btnJSON.onclick = function(){
    
    }
    
}
// End of window.onload!
